local skel = fk.CreateSkill {
  name = "#ready_skill_trigger",
}


---@class DIYUtility
local DIY = require "packages.diy_utility._base"



--- 获得蓄势技，增加蓄势小按钮
skel:addEffect(fk.EventAcquireSkill, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return target == player and data.skill:hasTag(DIY.ReadySkill)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "diy_ready_skill&", nil, false, true)
    -- 注意，此时机比Skill的onAcquire早
  end,
})

--- 游戏开始时，增加蓄势小按钮
-- 注意，全局触发技能添加到游戏比角色技能晚，无法检测开局添加的角色技能
skel:addEffect(fk.GamePrepared, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return not player:hasSkill("diy_ready_skill&", true) and
    table.find(player:getAllSkills(), function (s)
      return s:hasTag(DIY.ReadySkill)
    end)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "diy_ready_skill&", nil, false, true)
  end,
})

-- 失去蓄势技后，令其结束蓄势状态
-- 失去所有蓄势技后，删除蓄势小按钮
skel:addEffect(fk.EventLoseSkill, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return target == player and data.skill and data.skill:hasTag(DIY.ReadySkill)
  end,
  on_refresh = function (self, event, target, player, data)
    DIY.resetReadySkill(player, data.skill, "loseskill", true)
    if player:hasSkill("diy_ready_skill&", true) and not table.find(player:getAllSkills(),
      function (s) return s:hasTag(DIY.ReadySkill)
    end) then
      player.room:handleAddLoseSkills(player, "-diy_ready_skill&", nil, false, true)
    end
  end,
})

-- 洗牌后，重置蓄势
-- 在refresh触发新时机有点危险，但是无所谓了
skel:addEffect(fk.AfterDrawPileShuffle, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return table.find(player:getAllSkills(), function(s)
      return DIY.isReadying(player, s)
    end)
  end,
  on_refresh = function (self, event, target, player, data)
    for _, s in ipairs(player:getSkillNameList()) do
      if DIY.isReadying(player, s) then
        DIY.resetReadySkill(player, s, "shuffle")
      end
    end
  end,
})

-- 发动蓄势技时，令之开始蓄势
skel:addEffect(fk.SkillEffect, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return target == player and data.skill and data.skill:hasTag(DIY.ReadySkill) and not data.skill.is_delay_effect
    and not DIY.isReadying(player, data.skill)
  end,
  on_refresh = function (self, event, target, player, data)
    local skill = data.skill
    local skill_name = (skill:getSkeleton() or skill).name
    DIY.setReadySkill(player, skill_name, "useskill")
  end,
})


return skel
